# Game Demo
# Testing pygame basic features
# Use the SPACEBAR key and the DOWN key
# Haroon Khalid - 01/17/2009

import pygame
import sys

# PLAYER
class Player(object):
    def __init__(self):
        self.image = playerimg
        self.rect = self.image.get_rect()
        self.rect.x = 0
        self.rect.y = 0
        self.hits = 0

# RAYS
class Ray(object):
    def __init__(self):
        self.image = rayimg
        self.rect = self.image.get_rect()
        self.rect.x = 240
        self.rect.y = 43
        
    def update(self):
        self.rect.x -= 5
        screen.blit(self.image, (self.rect.x, self.rect.y))
        
# INITIALIZE PYGAME
pygame.init()

# SET WINDOW CAPTION
pygame.display.set_caption("Collision Test")

# SET WINDOW RESOLUTION
resolution = (300, 100)
screen = pygame.display.set_mode(resolution, 0, 32)

# SET UP THE FONT AND COLOR
default_font = pygame.font.get_default_font()
font = pygame.font.SysFont(default_font, 20)
white = (255,255,255)

# SETTING UP SPRITES
player = 'player.png'
playerimg = pygame.image.load(player).convert()
playerimg.set_colorkey((0,0,0))
player2 = 'player2.png'
player2img = pygame.image.load(player2).convert()
player2img.set_colorkey((0,0,0))
bg = 'bg.png'
bgimg = pygame.image.load(bg).convert()
ray = 'ray.png'
rayimg = pygame.image.load(ray).convert()
rayimg.set_colorkey((0,0,0))

# SETTING UP SOUNDS
raysound = pygame.mixer.Sound('ray.ogg')
owsound = pygame.mixer.Sound('ow.ogg')

# CREATE PLAYER
p1 = Player()
p1.rect.x = 50
p1.rect.y = 23

# LIST FOR STORING RAYS
rays = []
            
# GET INPUT
def test_input():
    for event in pygame.event.get():
        p1.image = playerimg
        p1.rect.y = 23
        # p1.rect.x was already defined above, so I removed it here
        # it caused problems with movement.
        
        if event.type == pygame.QUIT:
            sys.exit()
        
        elif event.type == pygame.KEYDOWN:        
            if event.key == pygame.K_DOWN:
                p1.image = player2img
                p1.rect.y = 55
            elif event.key == pygame.K_SPACE:
                shoot()
            elif event.key == pygame.K_LEFT:
                p1.rect.x -= 30
            elif event.key == pygame.K_RIGHT:
                p1.rect.x += 30
            else:
                p1.image = playerimg
                p1.rect.x = 50
                p1.rect.y = 23

# DRAW STATS AT TOP OF SCREEN                
def draw_stats():
    white = (255, 255, 255)
    hitamt = str(p1.hits)
    hit_txt = font.render("Hits: " + hitamt, True, white)
    screen.blit(hit_txt, (1, 1))

# COLLISION DETECTION
def check_hit():
    for i in rays:
        if i.rect.colliderect(p1.rect):
            rays.remove(i)
            p1.hits += 1
            owsound.play()

# SHOOT A LASER
def shoot():
    raysound.play()
    r = Ray()
    rays.append(r)

# UPDATE ALL LASERS    
def shoot_update():
    for i in rays:
        i.update()
    
# MAIN GAME LOOP
while True:
    test_input()
    screen.blit(bgimg, (0,0))
    screen.blit(p1.image, (p1.rect.x, p1.rect.y))
    check_hit()
    shoot_update()
    draw_stats()
    pygame.display.update()
    pygame.time.delay(25)
